<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 支付接口
 */
namespace app\allwin\controller\api\v4;
use app\allwin\controller\api\Base;
use app\allwin\model\AllwinUserAgent;
use app\allwin\model\AllwinConfig;
use app\common\model\SystemMemberForm;
use app\common\facade\WechatPay;

class Agentdopay extends Base{
   
    /**
     * 初始化当前应用是否登录
     * @return void
     */
    public function initialize() {
        parent::initialize();
        if(!$this->user){
            exit(json_encode(['code'=>404,'msg'=>'用户认证失败']));
        }
    }

    /**
     * 开通社群主
     */
    public function agent(){
        if (request()->isPost()) {
            $data = [
                'member_miniapp_id' => $this->miniapp_id,
                'uid'               => $this->user->id,
                'order_no'          => $this->request->param('order_no/s'),
            ];
            $validate = $this->validate($data, 'Dopay.agent');
            if (true !== $validate) {
                return enjson(403,$validate);
            }
            $order = AllwinUserAgent::where(['member_miniapp_id' => $this->miniapp_id,'uid' => $this->user->id,'pay_state' => 1])->find();
            if($order){
                return enjson(403,'请不要重复开通社群主');
            }
            $setting = AllwinConfig::getConfig($this->miniapp_id);
            $order_no = empty($data['order_no']) ? 'AGENT'.order_no() :$data['order_no'];
            if(empty($setting->agent_on_amount)){
                $data['pay_state'] = 2;
                $rel = AllwinUserAgent::insertOrder($data,$order_no);
                if($rel){
                    return enjson(200,'成功');
                }else{
                    return enjson(204,'开通社群主失败');
                }
            }
            //唤醒微信支付参数
            $payparm = [
                'openid'     => $this->user->miniapp_uid,
                'miniapp_id' => $this->miniapp_id,
                'name'       => '社群主开通',
                'order_no'   => $order_no,
                'total_fee'  => empty($data['order_no']) ? $setting->agent_on_amount * 100 : $order->price * 100,
                'notify_url' => api(1,'allwin/agentNotify/agent',$this->miniapp_id),
            ];
            //读取配置
            $setting = model('AllwinConfig')->getConfig($this->miniapp_id);
            if($setting->is_psp == 1){
                $default_mchid = model('MchId')->getMch(0, $this->miniapp_id);  //默认收款账户
                if (empty($default_mchid)) {
                    return enjson(403,'未找到商户号');
                }
                $payparm['mchid'] = $default_mchid->mchid;
            }
            $ispay = WechatPay::orderPay($payparm);
            if ($ispay['code'] == 0) {
                return enjson(403,$ispay['msg']);
            }
            //创建记录
            $data['price'] = $setting->agent_on_amount;
            empty($data['order_no']) ?: $rel = AllwinUserAgent::insertOrder($data,$order_no);
            if($rel){
                //把支付packageID添加到数据库
                $package = $ispay['data']['package'];
                SystemMemberForm::addForm($this->miniapp_id,$this->user->id,substr($package,10));
                return enjson(200,'成功',$ispay['data']);
            }else{
                return enjson(204,'开通社群主失败');
            }
        }
    }
}